package com.johann.multithread.runnable;
/**
 * 使用同步方法解决实现Runnable接口的线程安全问题
 *
 * 关于同步方法的总结:
 *  1. 同步方法仍然涉及到同步监视器，只是不需要我们显式的声明。
 *  2. 非静态的同步方法，同步监视器是：this
 *     静态的同步方法，同步监视器是：当前类本身
 */

public class RunnableSafety02SynchronizedMethod {
    public static void main(String[] args) {

        SellTickets02 sellTickets02 = new SellTickets02();
        Thread t1 = new Thread(sellTickets02);
        Thread t2 = new Thread(sellTickets02);
        Thread t3 = new Thread(sellTickets02);
        t1.start();
        t2.start();
        t3.start();
    }
}

class SellTickets02 implements Runnable{

    private static int ticketSum = 100;
    @Override
    public void run() {
        show();
    }

    private synchronized void show(){
        for (;ticketSum>0; ticketSum--) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+":"+ticketSum);
        }
    }

}